/**
 * noisetube javascript fns
 * User: Kennedy Kambona
 * Date: 12/01/13
 * Time: 22:35

document.observe("dom:loaded", function() {
    // the element in which we will observe all clicks and capture
    // ones originating from pagination links
    var container = $(document.body)
                            s
    if (container) {
        var img = new Image
        img.src = '/images/icons/spinner.gif'

        function createSpinner() {
            new Element('img', { src: img.src, 'class': 'spinner' })
        }

        container.observe('click', function(e) {
            var el = e.element()
            if (el.match('.pagination a')) {
                el.up('.pagination').insert(createSpinner())
                new Ajax.Request(el.href, { method: 'get' })
                e.stop()
            }
        })
    }
}) */

function pluralize(item_size, word){

    if (String.pluralize){
           item_size == 1 ? word : word.pluralize();
    } else {
        return  item_size == 1? word : word + "s";
    }
}

//ajax loader
function ajaxLoader (el, options) {
    // Becomes this.options
    var defaults = {
        bgColor 		: '#fff',
        duration		: 800,
        opacity			: 0.7,
        classOveride 	: false
    }
    this.options 	= jQuery.extend(defaults, options);
    this.container 	= $(el);
    this.init = function() {
        var container = this.container.parent() || this.container;
        // Delete any other loaders
        this.remove();
        // Create the overlay
        var overlay = $('<div></div>').css({
            'background-color': this.options.bgColor,
            'opacity':this.options.opacity,
            'width':container.width(),
            'height':container.height(),
            'position':'relative',
            'top':'0px',
            'left':'0px',
            'z-index':99999
            }).addClass('ajax_overlay');
        // add an overiding class name to set new loader style
        if (this.options.classOveride) {
            overlay.addClass(this.options.classOveride);
        }
        // insert overlay and loader into DOM
        container.append(
            overlay.append(
                $('<div></div>').addClass('ajax_loader')
            ).fadeIn(this.options.duration)
        );
    };
    this.remove = function(){
            var overlay = this.container.children(".ajax_overlay");
            if (overlay && overlay.length) {
                overlay.fadeOut(this.options.classOveride, function() {
                    overlay.remove();
            });
        }
    }
    this.init();
}
